<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *挪车记录
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
if($_RQ['op']=='list'){
	$params=array('type'=>1,'status'=>0);
	$params['uniacid']=$_SESSION['uniacid'];
	if(!empty($_RQ['merchid'])){
		$params['merchid']=intval($_RQ['merchid']);
		$return['params']['merchid']=$_RQ['merchid'];
	}
	$kulog=pdo_getall('jy_yxgoodserpfw_merchlog',$params,$fields,'','id desc');
	if($kulog){
		$list=array();
		foreach($kulog as $item){
			$item['total']=$item['code_type']?$item['xiang_num']:$item['code_num'];
			$list[$item['tomid']]['kulog'][]=$item;
			$list[$item['tomid']]['id']=$item['tomid'];
			$list[$item['tomid']]['tomid']=$item['tomid'];
			$list[$item['tomid']]['merchname']=pdo_getcolumn('core_members',array('id'=>$item['tomid'],'uniacid'=>$_SESSION['uniacid']),'nickname');
		}
		foreach($list as &$lv){
			$lv['desc']='内含'.count($list[$item['merchid']]['kulog']).'条出库记录';
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'待生成发货记录'),
		),
		'list'=>array('data'=>'list','edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
			array('name'=>'merchid','title'=>'经销商ID','type'=>'string'),
			array('name'=>'merchname','title'=>'经销商','type'=>'string'),
			array('name'=>'desc','title'=>'出库信息','type'=>'string'),
		),'action'=>array(
			array('type'=>'modal','op'=>'kulog','title'=>'出库记录'),
			array('type'=>'post','op'=>'addfalog','title'=>'生成发货单'),
		)),
		'search'=>array(
			array('type'=>'choose','width'=>13,'title'=>'经销商','name'=>'merchid','action'=>'jy_merch/get_merch/'),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='kulog'){
	$list=$this->getall('kulog',array('type'=>1,'status'=>0,'tomid'=>intval($_RQ['id']),'uniacid'=>$_SESSION['uniacid']));
	if(!$list){
		exi('指定经销商不存在出库记录！','error');
	}
	foreach($list as &$item){
		$item['total']=$item['code_type']?$item['xiang_num']:$item['code_num'];
		$item['merchname']=pdo_getcolumn('jy_merch_user',array('id'=>$item['merchid'],'uniacid'=>$_SESSION['uniacid']),'merchname');
	}
	$return['list']=$list;
	$htmls=array(
		'type'=>'list',
		'list'=>array('data'=>'list','edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
			array('name'=>'id','title'=>'ID','type'=>'string'),
			array('name'=>'merchname','title'=>'经销商','type'=>'string'),
			array('name'=>'sn','title'=>'流水号','type'=>'string'),
			array('name'=>'code','title'=>'防伪码','type'=>'string'),
			array('name'=>'total','title'=>'数量','type'=>'string'),
			array('name'=>'code_type','title'=>'类型','type'=>'radio','options'=>'code_type'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
			array('name'=>'createtime','title'=>'时间','type'=>'time'),
		)),
	);
	$return['code_type']=$this->code_type;
	$return['status']=array('待生成发货单','已生成发货单');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='addfalog'){
		$post=get_postdata();
		$list=$this->getall('merchlog',array('type'=>1,'status'=>0,'tomid'=>intval($_RQ['id']),'uniacid'=>$_SESSION['uniacid']));
		if(!$list){
			exi('指定经销商不存在出库记录！','error');
		}
		foreach($list as &$item){
			$item['total']=$item['code_type']?$item['xiang_num']:$item['code_num'];
			$item['merchname']=pdo_getcolumn('jy_merch_user',array('id'=>$item['merchid'],'uniacid'=>$_SESSION['uniacid']),'merchname');
		}
		$merch=pdo_get('core_members',array('id'=>$_RQ['id'],'uniacid'=>$_SESSION['uniacid']));
		$falog=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>intval($_RQ['id']),
			'kulog'=>ijson_encode($list),
			'address'=>'',
			'realname'=>$merch['realname']?:$merch['nickname'],
			'mobile'=>$merch['mobile'],
			'createtime'=>time(),
		);
		$this->insert('merchfalog',$falog);
		$this->update('merchlog',array('status'=>1),array('type'=>1,'status'=>0,'merchid'=>intval($_RQ['id']),'uniacid'=>$_SESSION['uniacid']));
		exi('生成发货单成功！','','/merch.falog/');
}